<?php

/**
 * UsuarioController
 * 
 * @package Sgit 
 * @author Juan Manuel Martinez Carrillo
 * @copyright Juan Manuel Martinez Carrillo
 * @version 2012
 * @access public
 * @revision 15/02/2012 16:04:37
 * Adaptar a Sgit_Controller_Action
 * Problemas con los grupos
 * $this->id $this->_id
 * Quito user sistema = del listado
 */
class UsuarioController extends Sgit_Controller_Action
{

    protected $_main_model = 'Usuario';
    protected $_main_form = 'Usuario';


    protected function _begin()
    {
        $this->_loadModel('AclGroup');
        $this->_loadModel('AclRolesUser');
    }

    public function indexAction()
    {

        $rows = $this->_model->default->getAll(array('id!= ?'=>0));
        $this->view->rows = $this->_prepareRowsToTr($rows);

    }

    public function editarAction()
    {
        $this->_editar();
        $this->_updateAcl();
    }

    public function insertarAction()
    {
        $this->_insertar();
        $this->_updateAcl();
    }

    public function borrarAction()
    {
        $this->_borrar();
        $this->_updateAcl();
    }

    public function personalAction()
    {

        $this->_id = $this->_auth->getIdentity()->id;

        $this->view->grupos = '<h3>Roles</h3>' . $this->_formatGrupos($this->_model->AclGroup->getGrupoByIdUser($this->_id));

        $this->_loadForm('Usuario');

        $this->_form->removeElement('grupos');
        $this->_form->removeElement('estado');
        
        $this->_reload();
        $this->_editar();

    }

    protected function _afterBorrar()
    {
        
        if ($this->_id)
            $this->_model->AclRolesUser->deleteRow(array("id_user=?"=>$this->_id));
    }

    protected function _afterEdit()
    {
        if($this->_id==$this->_auth->getIdentity()->id){
             $row = $this->_model->default->getRow($this->_auth->getIdentity()->id);
             $this->_auth->getIdentity()->nombre=$row['nombre'];
             $this->_auth->getIdentity()->locale=$row['locale'];
             $this->_auth->getIdentity()->mail=$row['mail'];
             
             
        }

    }

    
    protected function _afterAddEdit()
    {
        //si viene de edicion personal no hace nada con los grupos
        if($this->_actionName=='personal') return true;

        if ($this->_id)
            $this->_model->AclRolesUser->deleteRow(array("id_user=?"=>$this->_id));

        $values = $this->_form->getValues();
        if ($values['estado'] == 0)
        {
            $values['grupos'] = array();
        }

        //crea todos los roles
        foreach ((array )$values['grupos'] as $c)
        {
            
            $record['id_user'] = $this->_id;
            $record['id_group'] = $c;
            if(!$this->_id || !$c){
                throw new Zend_Controller_Exception("Es necesario el identificador de fila en el metodo _afterAddEdit");
            }

            $this->_model->AclRolesUser->insertRow($record);
        }
        return true;
    }

    protected function _prepareValuesToDb()
    {
        $values = $this->_form->getValues();
        unset($values['grupos']);
        return $values;
    }

    protected function _prepareValuesToForm($row)
    {

        if ($row)
            $row['grupos'] = $this->_model->AclGroup->getIdGrupoByIdUser($row['id']);
        return $row;
    }

    protected function _prepareValuesToTr($row)
    {
        $row['estado'] =Sgit_Util_SiNo::run($row['estado']);
        if ($row)
            $row['grupos'] = $this->_formatGrupos($this->_model->AclGroup->getGrupoByIdUser($row['id']));
        return $row;
    }

    protected function _formatGrupos($grupos)
    {
        $g = '';
        foreach ((array )$grupos as $v)
        {
            $g .= sprintf("%s<br>", $this->view->escape($v));
        }
        return $g;
    }


}
